﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LantinxShare.LBS;
using LantixLBSServer.Web.DbAccess.Helper;
using System.Data;

namespace LantixLBSServer.Web.DbAccess
{
    public class WorkItemManager : IManager
    {
//        public List<GWorkItem> GetWorkItemList()
//        {
//            List<GWorkItem> list = new List<GWorkItem>();
////            SqliteDB db = this.GetDB();
////            Execute(() =>
////            {
////                IDataReader reader = db.ExecuteDataReader(@"select t1.id,t1.username,t1.password,t1.useralias,t1.phone,
////                        t2.id,t2.roleid,t2.rolename from t_user t1,t_role t2 where t1.roleid= t2.roleid", false);
////                while (reader.Read())
////                {
////                    GWorkItem workItem = new GWorkItem()
////                    {

////                        Id = reader.GetInt32(0),
////                        WorkItemType  = new GWorkItemType()
////                    };
////                    list.Add(workItem);
////                }
////            }, () => { db.CloseConnection(); });


//            return list;
//        }

        public bool UpdateWorkItem(int id,int state)
        {
            bool result = false;
            SqliteDB db = this.GetDB();

            Execute(() =>
            {
                db.AddParameter("@id");
                db.AddParameter("@state");
                db.SetParameterValue("@state", state);
                db.SetParameterValue("@id", id);

                if (db.ExecuteNonQuery(@"update t_workitem set state=@state where id=@id ", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }

        public bool InsertWorkItem(int taskId,GWorkItem workItem)
        {
            bool result = false;
            SqliteDB db = this.GetDB();

            Execute(() =>
            {
                db.AddParameter("@taskId");
                db.AddParameter("@workItemTypeId");
                db.AddParameter("@State");

                db.SetParameterValue("@taskId", taskId);
                db.SetParameterValue("@workItemTypeId", workItem.WorkItemType.Id);
                db.SetParameterValue("@State", workItem.State);

                if (db.ExecuteNonQuery(@"insert into t_workitem(taskId,workItemTypeId,State) values( @taskId,@workItemTypeId,@State)", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }

        public bool DeleteWorkItem(int id)
        {
            bool result = false;
            SqliteDB db = this.GetDB();
            Execute(() =>
            {
                db.AddParameter("@id");
                db.SetParameterValue("@id", id);

                if (db.ExecuteNonQuery(@"delete from t_workitem where id=@id ", false, true) > 0)
                {
                    result = true;
                }
            });
            return result;
        }
    }
}